package mr.weblog.support;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

import com.google.common.collect.ComparisonChain;

public class TextPair implements WritableComparable<TextPair>{
    private Text first = new Text();
    private Text side = new Text();
    @Override
    public void write(DataOutput out) throws IOException {
        first.write(out);
        side.write(out);
    }
    @Override
    public void readFields(DataInput in) throws IOException {
        first.readFields(in);
        side.readFields(in);
    }
    @Override
    public int compareTo(TextPair o) {
        return ComparisonChain.start().
                compare(first, o.first).
                compare(side, o.side).
                result();
    }
    public Text getFirst() {
        return first;
    }
    public void setFirst(Text first) {
        this.first = first;
    }
    public Text getSide() {
        return side;
    }
    public void setSide(Text side) {
        this.side = side;
    }
    
    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
    
    @Override
	public int hashCode() {
		return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers
				append(first).append(side).toHashCode();
	}

    @Override
    public boolean equals(Object other){
    	if (other == null) 
    		return false;
        if (other == this) 
        	return true;
        if (!(other instanceof TextPair))
        	return false;
        
        TextPair otherTextPair = (TextPair)other;
        int ans = this.compareTo(otherTextPair);
        return (ans == 0);
    	
    }
    
}
